using System;
using System.Collections.Generic;
namespace NeuralNetwork
{
	public class Neuron{
		//not outputs, but this the collection of data and weights coming form the layer under this perceptron to this perceptron
		public List<Input> inputs = new List<Input>();
		public List<Input> outputs = new List<Input> ();
		public Layer layer;
		public double threshold;
		Function function;
		public double output;
		//unique ID in layer
		public int ID; 
		//error computed by backpropagation error
		public double error;
		
		public void getInput ()
		{
			foreach (Input input in inputs) {
                if (input.data == Double.NaN)
				input.data = input.fromPerceptron.output;	
			}	
		}

		public void computeOutput ()
		{
			this.function = new Function(this.threshold);
			this.output = function.logisticFunction (inputs);
            foreach (Input output in this.outputs)
            {
                output.data = this.output;
            }
            if (layer.name == "Output Layer")
            System.Console.WriteLine("Neuron ID:" + ID + "\tLayer:" + layer.name + "\tOutput:" + this.output);
		}
			
		public void addInputs (Input input)
		{
			this.inputs.Add (input);
		}
		
		public void addOutputs (Input output)
		{
			this.outputs.Add (output);
		}
		
		public Neuron ()
		{
			this.output = 0;
			
		}
		
		public Neuron (double output)
		{
			this.output = output;
		}
		
		public Neuron (Layer layer, int id)
		{
            Random random = new Random();
            double number;
            if (random.NextDouble() > .5)
                number = random.NextDouble();
            else
                number = -random.NextDouble();
                                 
			this.output = 0;
			this.ID = id;
			this.layer = layer;
			this.threshold = number;
			
		}
		
	}
}

